package org.jeecg.glr.service.impl;

import org.apache.commons.codec.digest.DigestUtils;
import org.jeecg.glr.entity.GlrResource;
import org.jeecg.glr.mapper.GlrResourceMapper;
import org.jeecg.glr.service.IGlrRadioServerService;
import org.jeecg.glr.service.IGlrResourceService;
import org.jeecg.glr.service.IGlrVideoYsService;
import org.jeecg.glr.util.GLRConstant;
import org.jeecg.glr.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.io.File;
import java.util.List;

/**
 * @Description: 资源表
 * @Author: jeecg-boot
 * @Date:   2020-02-11
 * @Version: V1.0
 */
@Service
public class GlrResourceServiceImpl extends ServiceImpl<GlrResourceMapper, GlrResource> implements IGlrResourceService {

    @Autowired
    private IGlrRadioServerService glrRadioServerService;

    @Override
    public boolean saveFile(GlrResource domain) {

        String[] datastr=domain.getFilePath().split(",");
        for(int i=0;i<datastr.length;i++){
            GlrResource r = new GlrResource();
            String[] datas=datastr[i].split("#");
            r.setFilePath(datas[0]);
            r.setFileName(datas[1]);
            r.setFileMd5(datas[2]);
            r.setFileId(datas[3]);
            r.setFileSize(Integer.parseInt(datas[4]));
            r.setFilePlayTime(Integer.parseInt(datas[5]));
            r.setFileType(datas[6]);
            r.setEquipmentType("1");
            r.setRattopFileId(datas[7]);

            Long l=Long.parseLong(datas[8]);
            Long rattopMediaId=0l;
            if (l==0){
                try {
                    //有时服务器计算mp3可以使用时间会慢于此接口
                    rattopMediaId = glrRadioServerService.getRattopMediaId(datas[7]);
                    if (rattopMediaId==0){
                        rattopMediaId = glrRadioServerService.getRattopMediaId(datas[7]);
                    }
                    if (rattopMediaId==0){
                        Thread.sleep(100);
                        rattopMediaId = glrRadioServerService.getRattopMediaId(datas[7]);
                    }
                    if (rattopMediaId==0){
                        Thread.sleep(100);
                        rattopMediaId = glrRadioServerService.getRattopMediaId(datas[7]);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            //System.out.println("================"+datas[7]+"================"+rattopMediaId+"================"+l);
            r.setRattopMediaId(rattopMediaId);
            r.setCompanyCode(datas[9]);
             super.save(r);
        }
        return true;
    }

    @Override
    public boolean saveLedFile(GlrResource domain) {
        String[] datastr=domain.getFilePath().split(",");
        for(int i=0;i<datastr.length;i++){
            GlrResource r = new GlrResource();
            String[] datas=datastr[i].split("#");
            r.setFilePath(datas[0]);
            r.setFileName(datas[1]);
            r.setFileMd5(datas[2]);
            r.setFileId(datas[3]);
            r.setFileSize(Integer.parseInt(datas[4]));
            r.setFilePlayTime(Integer.parseInt(datas[5]));
            r.setFileType(datas[6]);
            r.setEquipmentType(GLRConstant.GLR_LED_MEMBER_TYPE_2);
            super.save(r);
        }
        return true;
    }

}
